package com.atguigu.gmall.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.oms.entity.OrderItemEntity;
import com.atguigu.gmall.oms.feign.GmallPmsClient;
import com.atguigu.gmall.oms.mapper.OrderItemMapper;
import com.atguigu.gmall.oms.service.OrderItemService;
import com.atguigu.gmall.oms.vo.OrderItemVo;
import com.atguigu.gmall.oms.vo.OrderSubmitVo;
import com.atguigu.gmall.pms.entity.*;
import com.atguigu.gmall.ums.entity.UserAddressEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.oms.mapper.OrderMapper;
import com.atguigu.gmall.oms.entity.OrderEntity;
import com.atguigu.gmall.oms.service.OrderService;
import org.springframework.util.CollectionUtils;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {


    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private GmallPmsClient pmsClient;
    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<OrderEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<OrderEntity>()
        );

        return new PageResultVo(page);
    }

    //新增订单信息 订单表与订单详情表
    @Override
    public void saveOrder(OrderSubmitVo submitVo, Long userId) {
        OrderEntity orderEntity = new OrderEntity();
        //设置order表信息
        orderEntity.setUserId(userId);
        orderEntity.setOrderSn(submitVo.getOrderToken());
        orderEntity.setCreateTime(new Date());
        orderEntity.setTotalAmount(submitVo.getTotalPrice());
        orderEntity.setPayAmount(submitVo.getTotalPrice());
        orderEntity.setSourceType(0);
        orderEntity.setDeleteStatus(0);
        orderEntity.setUseIntegration(submitVo.getBounds());
        //为支付 订单状态
        orderEntity.setStatus(0);
        orderEntity.setDeliveryCompany(submitVo.getDeliveryCompany());
        UserAddressEntity address = (UserAddressEntity) submitVo.getAddress();
        if (address != null){
            //设置详细地址信息
            orderEntity.setUsername(address.getName());
            orderEntity.setReceiverName(address.getName());
            orderEntity.setReceiverPhone(address.getPhone());
            orderEntity.setReceiverPostCode(address.getPostCode());
            orderEntity.setReceiverProvince(address.getProvince());
            orderEntity.setReceiverCity(address.getCity());
            orderEntity.setReceiverRegion(address.getRegion());
            orderEntity.setReceiverAddress(address.getAddress());
        }
        //保存信息
        this.save(orderEntity);

        //新增订单详情信息 item集合转换为orderItem
        List<OrderItemVo> items = submitVo.getItems();
        if (!CollectionUtils.isEmpty(items)){
            items.forEach(item->{
                OrderItemEntity orderItemEntity = new OrderItemEntity();
                orderItemEntity.setOrderId(orderEntity.getId());
                orderItemEntity.setOrderSn(submitVo.getOrderToken());

                //根据skuId查询sku信息
                ResponseVo<SkuEntity> skuEntityResponseVo = pmsClient.querySkuById(item.getSkuId());
                SkuEntity skuEntity = skuEntityResponseVo.getData();
                if (skuEntity != null) {
                    Long skuId = skuEntity.getId();
                    orderItemEntity.setSkuId(skuId);
                    orderItemEntity.setSkuName(skuEntity.getName());
                    //设置sku图片信息
                    ResponseVo<List<SkuImagesEntity>> skuImagesResponseVo = pmsClient.queryImagesBySkuId(skuId);
                    List<SkuImagesEntity> skuImagesEntities = skuImagesResponseVo.getData();
                    if (!CollectionUtils.isEmpty(skuImagesEntities)){
                        //获取图片信息
                        orderItemEntity.setSkuPic(JSON.toJSONString(skuImagesEntities));
                    }
                    orderItemEntity.setSkuPrice(skuEntity.getPrice());
                    //设置购买数量
                    orderItemEntity.setSkuQuantity(item.getCount().intValue());

                    //设置销售属性
                    ResponseVo<List<SkuAttrValueEntity>> skuAttrsResponseVo = pmsClient.querySkuAttrsBySkuId(skuId);
                    List<SkuAttrValueEntity> skuAttrValueEntities = skuAttrsResponseVo.getData();
                    if (!CollectionUtils.isEmpty(skuAttrValueEntities)){
                        orderItemEntity.setSkuAttrsVals(JSON.toJSONString(skuAttrValueEntities));
                    }

                    //设置spu信息
                    Long spuId = skuEntity.getSpuId();
                    ResponseVo<SpuEntity> spuEntityResponseVo = pmsClient.querySpuById(spuId);
                    SpuEntity spuEntity = spuEntityResponseVo.getData();
                    if (spuEntity != null) {
                        orderItemEntity.setSpuId(spuId);
                        orderItemEntity.setSpuName(spuEntity.getName());

                        //设置spu图片信息 也就是描述信息
                        ResponseVo<SpuDescEntity> spuDescEntityResponseVo = pmsClient.querySpuDescById(spuId);
                        SpuDescEntity spuDescEntity = spuDescEntityResponseVo.getData();
                        if (spuDescEntity != null) {
                            orderItemEntity.setSpuPic(spuDescEntity.getDecript());
                        }

                        //设置品牌信息
                        ResponseVo<BrandEntity> brandEntityResponseVo = pmsClient.queryBrandById(skuEntity.getBrandId());
                        BrandEntity brandEntity = brandEntityResponseVo.getData();
                        if (brandEntity != null) {
                            orderItemEntity.setSpuBrand(JSON.toJSONString(brandEntity));
                        }
                    }
                }
                //保存订单详情信息
                orderItemMapper.insert(orderItemEntity);
            });
        }
    }
}